package com.redrose.flutterdemo.ui.fragments;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import com.redrose.flutterdemo.R;
import com.redrose.flutterdemo.ui.activitys.BaseActivity;
import me.yokeyword.fragmentation.SupportFragment;
import com.redrose.flutterdemo.databinding.FragmentBaseBinding;

/**
 * Desc:
 *
 * @author: RedRose
 * Date: 2019/5/19 0019
 * Email: yinsxi@163.com
 */
public abstract class BaseFragment<T extends ViewDataBinding> extends SupportFragment {
    /**
     * 需要实现的设置layout的抽象方法
     */
    public abstract int setLayout();

    private FragmentBaseBinding mBaseBinding;

    protected T mBinding;

    public abstract void initData();

    public abstract void initEvent();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBaseBinding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.fragment_base, null, false);
        mBinding = DataBindingUtil.inflate(getLayoutInflater(), setLayout(), null, false);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        DataBindingUtil.inflate(getLayoutInflater(), R.layout.fragment_base, null, false);
        mBinding.getRoot().setLayoutParams(params);
        mBaseBinding.container.addView(mBinding.getRoot());
        initData();
        initEvent();
        return mBaseBinding.getRoot();
    }

    /**
     * 返回唯一的Activity实例
     */
    public final BaseActivity getProxyActivity() {

        return (BaseActivity) _mActivity;
    }

    /**
     * 通过Class跳转界面
     **/
    public void startActivity(Class<?> cls) {
        startActivity(cls, null);
    }

    /**
     * 通过Class跳转界面
     **/
    public void startActivityForResult(Class<?> cls, int requestCode) {
        startActivityForResult(cls, null, requestCode);
    }

    /**
     * 含有Bundle通过Class跳转界面
     **/
    public void startActivityForResult(Class<?> cls, Bundle bundle,
                                       int requestCode) {
        Intent intent = new Intent();
        intent.setClass(getActivity(), cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, requestCode);
    }

    /**
     * 含有Bundle通过Class跳转界面
     **/
    public void startActivity(Class<?> cls, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(getActivity(), cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

}
